﻿using Core.DataAccess.Model.Project.Entrepot;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IEntrepotModule;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.Web.Controllers.Entrepot
{
    /// <summary>
    /// 产品属性操作
    /// </summary>
    [Route(".v1/Entrepot/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class ProductAttributeController : BaseApiController
    {
        public ProductAttributeController(IEntrepotOperation iEntrepotOperation) :
            base(iEntrepotOperation: iEntrepotOperation)
        { }
        

        /// <summary>
        /// 获取指定属性列表
        /// </summary>
        /// <returns></returns>
        [HttpPost(".page")]
        public IResult GetAttribute(AttributeListRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepotOperation.GetAttributes(
                        iProjectInfo.Token,
                        model.attributes,
                        model.pagination
                        ), model.pagination);
                });
        }

        /// <summary>
        /// 添加产品属性
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IResult PostAttr(ProductAttrbute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepotOperation.OperationAttrbute(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改产品属性
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IResult PutAttr(ProductAttrbute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepotOperation.OperationAttrbute(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除产品属性
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult DeleteAttr(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepotOperation.OperationAttrbute(
                        new ProductAttrbute
                        {
                            Id = id
                        },
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }
    }
}
